package com.dogsong.liteflow.editor.controller;

import com.dogsong.liteflow.editor.bean.CmpProperty;
import com.dogsong.liteflow.editor.bean.ELInfo;
import com.dogsong.liteflow.editor.parser.generator.ExpressGenerator;
import com.yomahub.liteflow.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

/**
 * ApiController
 *
 * @author <a href="mailto:dogsong99@163.com">dosong</a>
 * @since 2024/4/18
 */
@Api(tags = "EL转换接口")
@RequestMapping("/api")
@RestController
@Slf4j
public class GenerateELController {

    @Autowired
    private ExpressGenerator expressGenerator;

    @ApiOperation(value = "EL表达式转成JSON结构", notes = "输入EL表达式")
    @ApiImplicitParam(name = "elInfo", value = "EL语句", required = true)
    @PostMapping("/generateJsonEL")
    @ResponseBody
    public CmpProperty generateJsonEL(@Nullable @RequestBody ELInfo elInfo) {
        log.info("generateJsonEL 收到数据: {}", JsonUtil.toJsonString(elInfo));
        CmpProperty vo = new CmpProperty();
        try {
            vo = expressGenerator.generateJsonEL(elInfo);
        } catch (Exception ex) {
            log.error("-----", ex);
        }
        log.info("generateJsonEL 生成数据: {}", JsonUtil.toJsonString(vo));
        return vo;
    }

    @ApiOperation(value = "JSON结构转EL表达式", notes = "输入JSON结构")
    @ApiImplicitParam(name = "jsonEl", value = "json结构", required = true)
    @PostMapping("/generateEL")
    @ResponseBody
    public ELInfo generateEL(@Nullable @RequestBody CmpProperty jsonEl) {
        log.info("generateEL 收到数据: {}", JsonUtil.toJsonString(jsonEl));
        ELInfo vo = new ELInfo();
        try {
            vo = expressGenerator.generateEL(jsonEl);
        } catch (Exception ex) {
            log.error("-----", ex);
        }
        log.info("generateEL 生成数据: {}", JsonUtil.toJsonString(vo));
        return vo;
    }

}
